package cn.t.service.user;

import cn.t.core.exception.BizException;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.service.ServiceImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.enums.user.UserError;
import cn.t.model.user.UserInfoDO;
import cn.t.model.user.UserQueryDO;
import cn.t.repository.user.UserInfoRepository;
import cn.t.utils.AESUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户信息表领域服务
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserInfoService extends ServiceImpl<UserInfoDO, UserInfoRepository> {


    public void checkPassword(Long userId, String password) {

        UserInfoDO userInfo = repository.findById(userId);
        if (userInfo == null || userInfo.getId() == null || !userInfo.getId().equals(userId)) {
            log.warn("用户名不存在 用户ID：{}", userId);
            throw new BizException(UserError.PASSPORT_IS_NOT_EXISTS, UserError.PASSPORT_IS_NOT_EXISTS.getName());
        }

        if (!String.valueOf(BaseStatusEnum.AVAILABLE.getCode()).equals(userInfo.getUserStatus())
                || !YesNoEnum.FALSE.equals(userInfo.getIsDeleted())) {
            log.warn("用户状态不可用 用户ID：{}", userId);
            throw new BizException(UserError.PASSPORT_IS_NOT_EXISTS, UserError.PASSPORT_IS_NOT_EXISTS.getName());
        }

        String encodedPassword = AESUtil.encrypt(password, userId);
        if (!userInfo.getLoginPassword().equals(encodedPassword)) {
            log.warn("用户名密码校验失败 用户ID：{} 密码：{} {} {}", userId, password, encodedPassword, userInfo.getLoginPassword());
            throw new BizException(UserError.VALIDATE_PASSWORD_FAILED, UserError.VALIDATE_PASSWORD_FAILED.getName());
        }

    }

    public UserInfoDO createUser(UserInfoDO userInfoDO) {

        Long userId = addAndReturnId(userInfoDO);
        userInfoDO.setId(userId);

        // 更新 用户密码
        if (StringUtils.isBlank(userInfoDO.getLoginPassword())) {
            userInfoDO.setLoginPassword("888888");
        }

        String encryptedPassword = AESUtil.encrypt(userInfoDO.getLoginPassword(), userInfoDO.getId());
        userInfoDO.setLoginPassword(encryptedPassword);
        update(userInfoDO);

        return userInfoDO;
    }

    public void changePassword(Long id, String loginPassword) {

        UserInfoDO userInfoDO = repository.findById(id);
        if (userInfoDO == null || userInfoDO.getId() == null || !userInfoDO.getId().equals(id)) {
            log.warn("用户名不存在 用户ID：{}", id);
            throw new BizException(UserError.PASSPORT_IS_NOT_EXISTS, UserError.PASSPORT_IS_NOT_EXISTS.getName());
        }

        UserInfoDO template = new UserInfoDO();

        template.setId(id);
        template.setLoginPassword(AESUtil.encrypt(loginPassword, id));
        template.setGmtModified(new Date());

        update(template);
    }

    public boolean disableUser(Long userId) {

        UserInfoDO template = new UserInfoDO();
        template.setId(userId);
        template.setGmtModified(new Date());
        template.setUserStatus(String.valueOf(BaseStatusEnum.DISABLE.getCode()));

        return update(template);
    }

    public boolean enableUser(Long userId) {

        UserInfoDO template = new UserInfoDO();
        template.setId(userId);
        template.setGmtModified(new Date());
        template.setUserStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));

        return update(template);
    }

    public void deleteUser(Long userId) {
        repository.deleteUser(userId);
    }

    public UserQueryDO queryUserRule(Long deviceId, String userCarNum, Long merchantId) {
        return repository.queryUserRule(deviceId, userCarNum, merchantId);
    }


}
